<?php

namespace Rbac\Controller;

/**
  +----------------------------------------------------------
 * 课程管理控制器
  +----------------------------------------------------------
 * TeachReviewController
  +----------------------------------------------------------
 * @author Jonas_yang 2016-2-14
  +----------------------------------------------------------
 */
class TeachReviewController extends ExtendController {
    
    /**
     +----------------------------------------------------------
     * 简单插入处理，支持json返回和页面跳转返回
     +----------------------------------------------------------
     */
    public function insert() {
        $modelName = $_GET["cmd"];//获取控制器名称
        $model = D($modelName);  //根据名称找到对应Model
        if (false === $model->create()) {
            $data["success"] = 2;
            $data["msg"] = $model->getError();
        } else {
            $res = $model->add();
            if ($res !== false) {
                $data["success"] =1;
                $data["msg"] = "添加数据成功";
            } else {
                $data["success"] = 3;
                $data["msg"] = "添加数据失败";
            }
        }
        $this->ajaxReturn($data);
    }
    
    /**
     * 显示数据
     */
    public function index() {

        $modelName = $_GET["cmd"];
        $map = array();
        $filterFunction = "_filter" . $modelName;
        if (method_exists($this, $filterFunction)) {
            $map = $this->$filterFunction();
        }
        $model = D($modelName);
        $data = null;
        if (!empty($model)) {
            $data = $this->_list($model, $map);
        }

        if ($data["rows"] == null) {
            $data["rows"] = array();
        }
        $processFunction = "_process" . $modelName;
        if (method_exists($this, $processFunction)) {
            $data = $this->$processFunction($data);
        }
        //过滤数据
        $this->ajaxReturn($data);
    }
    
    
    //过滤数据
    public function _filterReviewTeacherInfo(){
        $map = array();
        if(!empty($_GET["is_me"])&&$_GET["is_me"]=="my"){
            $map["it_id"] = \Fn\App::getUserId();
        }
        if(!empty($_POST["cu_name"])){
            $map["cu_name"] = array("like",trim($_POST["cu_name"])."%");
        }
        return $map;
    }
    
    /**
     * 过滤数据
     * @return string
     */
    public function _filterReviewInfo(){
        $map = array();
        if(!empty($_GET["is_me"])&&$_GET["is_me"]=="my"){
            $map["it_id"] = \Fn\App::getUserId();
        }
        if(!empty($_POST["cu_name"])){
            $map["cu_name"] = array("like",trim($_POST["cu_name"])."%");
        }
        return $map;
    }

    /**
     * 创建评教信息
     */
    public function createReview() {
        $mcId = I("post.mc_id");  //专业课程Id
        $startTime = I("post.review_create_time");
        $endTime = I("post.review_end_time");
        $mgCurriculumModel = M("mg_curriculum");
        $map["mc_id"] = array("in", $mcId);
        $infoCurriculumModel = M("info_curriculum");
        $mgCurriculumList = $mgCurriculumModel->where($map)->select();
        $reviewInfoModel = M("review_info");
        $studentInfoModel = M("info_student");
        $reviewStudentInfoModel = M("review_student_info");
        $n = 0;
        foreach ($mgCurriculumList as $mgCurriculum) {
            $curriculum = $infoCurriculumModel->field("cu_name")->find($mgCurriculum["cu_id"]);
            //插入数据
            $data["it_id"] = $mgCurriculum["it_id"];
            $data["ico_id"] = $mgCurriculum["ico_id"];
            $data["id_id"] = $mgCurriculum["id_id"];
            $data["icl_id"] = $mgCurriculum["icl_id"];
            $data["mc_id"] = $mgCurriculum["mc_id"];
            $data["review_create_time"] = $startTime;
            $data["cu_name"] = $curriculum["cu_name"];
            $data["review_end_time"] = $endTime;
            $res = $reviewInfoModel->add($data);
            //修改状态
            if ($res !== false) {
                $mgCurriculumModel->where(array("mc_id" => $mgCurriculum["mc_id"]))
                    ->save(array("mc_status" => 9));
                //将这个专业班级的学生初始化进去
                $studentInfoList = $studentInfoModel->field("is_id")
                    ->where(array("id_id" => $data["id_id"],"icl_id"=>$data["icl_id"]))
                    ->select();
                foreach ($studentInfoList as $studentInfo) {
                    $reviewStudentInfo["review_id"] = $res;
                    $reviewStudentInfo["is_id"] = $studentInfo["is_id"];
                    $reviewStudentInfoModel->add($reviewStudentInfo);
                }
                $n++;
            }
        }
        $this->ajaxReturn(array("success" => 1, "msg" => "已创建" . $n . "条选课记录"));
    }

    public function teachReview() {
        $this->display();
    }
    
    public function teachreviewinfo() {
        $this->display();
    }
    
    public function myReviewInfo() {
        $this->display();
    }

    public function teachReviewStudentInfo() {
        //review_id

        $reviewId = $_GET["review_id"];
        $reviewStudentInfoModel = M("review_student_info");
        $map["review_id"] = $reviewId;
        if(!empty($_POST["is_id"])){
            $map["is_id"] = $_POST["is_id"];
        }
        //创建分页对象，是否设置行数
        if (!empty($_REQUEST['rows'])) {
            $pageSize = $_REQUEST['rows'];
        } else {
            $pageSize = "10";
        }
        if (!empty($_REQUEST['page'])) {
            $pageNow = $_REQUEST['page'];
        } else {
            $pageNow = "1";
        }
        $offset = $pageSize * ($pageNow - 1); //定义偏移量
        $reviewStudentInfo = $reviewStudentInfoModel->where($map)->limit($offset,$pageSize)->select();
        $studentModel = M("InfoStudent");
        $count = $reviewStudentInfoModel->where($map)->count();
        foreach ($reviewStudentInfo as $key => $info) {
            //获取学生信息
            $student = $studentModel->find($info["is_id"]);
            $info["is_name"] = $student["is_name"];
            $reviewStudentInfo[$key] = $info;
        }
        $data["rows"] = $reviewStudentInfo;
        $data["total"] = $count;
        $this->ajaxReturn($data);
    }

    /**
     * 教学评价列表
     */
    public function reviewlist() {
        //获取当前用户ID
        $map['is_id'] = $_SESSION[C('USER_AUTH_KEY')];
        $reviewStudentInfoModel = M("review_student_info");
        $reviewInfoModel = M("review_info");
        $mgCurriculumModel = M("mg_curriculum");
        $rbacUserModel = M("rbac_user");
        $reviewInfoList = $reviewStudentInfoModel->where($map)->select();
        $nowtime = time();
        foreach ($reviewInfoList as $key => $reviewInfo) {
            $review = $reviewInfoModel->find($reviewInfo["review_id"]);
            $cu = $mgCurriculumModel->field("cu_id")->find($reviewInfo["mc_id"]);
            //获取课程名称
            $reviewInfo["cu_id"] = $reviewInfo["review_id"];
            $reviewInfo["cu_name"] = $review["cu_name"];
            $reviewInfo["it_id"] = $review["it_id"];
            $user = $rbacUserModel->field("nickname")->find( $review["it_id"]);
            $reviewInfo["it_name"] = $user["nickname"];
            $reviewInfo["review_end_time"] = $review["review_end_time"];
            $startTime = strtotime($review["review_create_time"]);
            $endTime = strtotime($review["review_end_time"]);
            if($nowtime<$startTime){
                $reviewInfo["review_status"] = 2; //表示未开始
            }
            if($nowtime>$endTime){
                $reviewInfo["review_status"] = 5; //表示已结束
            }
            
            $reviewInfoList[$key] = $reviewInfo;
        }
        $this->assign("reviewInfoList", $reviewInfoList);
        $this->display();
    }
    
    /**
     * 评教标签
     */
    public function teachLabel(){
        $this->display("reviewLable");
    }

    /**
     * 评价详细
     */
    public function review() {
        $review_info_id = $_GET["id"];
        $reviewStudentInfoModel = M("review_student_info");
        $reviewInfoModel = M("review_info");
        $rbacUserModel = M("rbac_user");
        $reviewInfo = $reviewStudentInfoModel->field("review_id")->find($review_info_id);
        $userId  = \Fn\App::getUserId();
        if(empty($reviewInfo)&& empty($userId)){
            $this->display("public/loginout");exit;
        }else{
        $review = $reviewInfoModel->field("cu_name,it_id")->find($reviewInfo["review_id"]);
        $user = $rbacUserModel->field("nickname")->find( $review["it_id"]);
        $title = $user["nickname"]."-".$review["cu_name"]."评教";
        //获取教学标签
        $reviewLableModel = M("review_lable");
        $labelList = $reviewLableModel->select();
        $this->assign("title",$title);
        $this->assign("label_list",$labelList);
        $this->assign("review_info_id",$review_info_id);
        $this->display();
        
        }
    }

    /**
    *更新教师评价信息
    **/
    public function updateteacherreview(){
        //检查是否有到期的数据
        $reviewInfoModel = M("review_info");
        $reviewTeacherInfoModel = M("review_teacher_info");
        $reviewStudentInfoModel = M("review_student_info");
        $map = array(
            "review_end_time"=>array('ELT',date("Y-m-d H:i:s")),//<当前日期
            "review_status"=>1  //未结束
        );
        $reviewInfoList = $reviewInfoModel->where($map)->select();
        //改变表的状态，评教结束
        foreach($reviewInfoList as $reviewInfo){
              //应评教人数
              $count = $reviewStudentInfoModel->where(array("review_id"=>$reviewInfo["review_id"]))->count();
              //评教人数和分数
              $where = array("review_id"=>$reviewInfo["review_id"],
                             "review_status"=>9);
              $reviewCount = $reviewStudentInfoModel->where($where)->count(); //已评教
              $reviewSumGrade = $reviewStudentInfoModel->where($where)->sum('review_grade');
              $reviewSumGrade = intval($reviewSumGrade);
              //取出学生评教的分数
              if($reviewCount==0){
                  $reviewGrade = 0;
              } else {
                  $reviewGrade = round($reviewSumGrade/$reviewCount,2); //平均分数
              }
              $row["review_grade"] = $reviewGrade;//平均分数
              $row["review_count"] = $reviewCount;//已评教人数
              $row["reivew_origin_count"] = $count;//原本学生人数
              $row["review_sum_grade"] = $reviewSumGrade ;//总分数
              $row["it_id"] = $reviewInfo["it_id"];
              $row["ico_id"] = $reviewInfo["ico_id"];
              $row["cu_name"] = $reviewInfo["cu_name"];
              $row["id_id"] = $reviewInfo["id_id"];
              $row["icl_id"] = $reviewInfo["icl_id"];
              $row["review_id"] = $reviewInfo["review_id"];
              //$row["mc_id"] = $reviewInfo["mc_id"];
              $row["review_create_date"] = date("Y-m-d H:i:s");
              //检测review_id是否存在
              $r = $reviewTeacherInfoModel->where(array("review_id"=>$reviewInfo["review_id"]))->find();
              if(empty($r)){
                  $reviewTeacherInfoModel->add($row);
              }else{
                  $reviewTeacherInfoModel->where(array("review_id"=>$reviewInfo["review_id"]))->save($row);
              }
        }
        $this->ajaxReturn(array("success"=>true,"msg"=>"更新数据成功"));
    }
    
    
    /**
     * 学生评价
     */
    public function studentreview(){
        $rating = $_POST["rating"];
        $grade = (($rating*10)/5)*10;
        $model = M("review_student_info");

        $map["review_info_id"] = $_POST["review_info_id"];

        $data["review_status"] = 9;
        $data["review_grade"]  = $grade;
        $data["review_label_info"] = $_POST["lableId"];
        $data["review_note"] = $_POST["reviewNote"];
        $data["review_date"] = date("Y-m-d H:i:s");
        $res = $model->where($map)->save($data);
        if($res!==false){
            $this->ajaxReturn(array("success"=>true,"msg"=>"提交成功"));
        }else{
            $this->ajaxReturn(array("success"=>false,"msg"=>"评价失败"));
        }
    }

}
